﻿using System;
using System.Windows.Forms;
using ShadeTree.Binding;
using ShadeTree.Binding.Elements;

namespace ShadeTree.WinForms.Tests
{
    public interface IControlHandler
    {
        bool Handles(object control);
        bool IsChecked(object control);
        void Enter(object control, string text);
        object GetValue(IScreenElement element);
    }

    public class CheckboxHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is CheckBox;
        }

        public bool IsChecked(object control)
        {
            return ((CheckBox) control).Checked;
        }

        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            return IsChecked(element);
        }

        #endregion
    }

    public class LabelHandler : IControlHandler
    {
        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            System.Windows.Forms.Label label = (System.Windows.Forms.Label)element.Control;
            return label.Text;
        }

        public bool Handles(object control)
        {
            return control is System.Windows.Forms.Label;
        }

        public bool IsChecked(object control)
        {
            throw new NotImplementedException();
        }
    }

    public class WpfLabelHandler : IControlHandler
    {
        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            System.Windows.Controls.Label label = (System.Windows.Controls.Label)element.Control;
            return label.Content.ToString();
        }

        public bool Handles(object control)
        {
            return control is System.Windows.Controls.Label;
        }

        public bool IsChecked(object control)
        {
            throw new NotImplementedException();
        }
    }

    public class WpfCheckboxHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is System.Windows.Controls.CheckBox;
        }

        public bool IsChecked(object control)
        {
            return ((System.Windows.Controls.CheckBox)control).IsChecked.GetValueOrDefault(false);
        }

        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            return IsChecked(element);
        }

        #endregion
    }

    public class RadioButtonHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is RadioButton;
        }

        public bool IsChecked(object control)
        {
            return ((RadioButton) control).Checked;
        }

        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            return IsChecked(element);
        }

        #endregion
    }

    public class TextBoxHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is TextBox;
        }

        public bool IsChecked(object control)
        {
            throw new NotImplementedException();
        }

        public void Enter(object control, string text)
        {
            ((TextBox) control).Text = text;
            TestDriver.FireEvent(control, "Validated");
        }


        public object GetValue(IScreenElement element)
        {
            return ((TextBox) element.Control).Text;
        }

        #endregion
    }

    public class ComboBoxHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is ComboBox;
        }

        public bool IsChecked(object control)
        {
            throw new NotImplementedException();
        }

        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            var listElement = (IListElement) element;
            return listElement.DisplayValue;
        }

        #endregion
    }

    public class WpfComboBoxHandler : IControlHandler
    {
        #region IControlHandler Members

        public bool Handles(object control)
        {
            return control is System.Windows.Controls.ComboBox;
        }

        public bool IsChecked(object control)
        {
            throw new NotImplementedException();
        }

        public void Enter(object control, string text)
        {
            throw new NotImplementedException();
        }

        public object GetValue(IScreenElement element)
        {
            var listElement = (IListElement)element;
            return listElement.DisplayValue;
        }

        #endregion
    }
}